package com.nf.health.app.myfamily.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.OldSosAdapter;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.adapter.OnMyClickListener1;
import com.nf.health.app.customview.MyListView;
import com.nf.health.app.utils.ViewUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 
 * @ClassName: OldSettongSosActivity 
 * @Description: 老人机设置sos
 * @author Ian 
 * @date 2015-11-18 下午3:20:13 
 *  
 */
public class OldSettongSosActivity extends BaseActivity {

	private LinearLayout oldcall_lin;


	private Map<String, String> isCheckMap;
	private Map<String, String> isCheckMapadd = new HashMap<String, String>();

	private Button next_btn;

	private EditText num,phonenum,name;

	private MyListView oldcall_listview;

	String name1,phonenum1;

	private String imei;

	List list = new ArrayList();

	private OldSosAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_old_settong_sos);
		ViewUtil.setText(this, R.id.titlebar_title_tv, "SOS呼叫号码");


		initView();

		imei = getIntent().getStringExtra("imei");
		provider.requestOldCall(imei,"OldSos");

	}
	private void initView() {
		// TODO Auto-generated method stub
		next_btn = (Button) findViewById(R.id.next_btn);//保存按钮

		oldcall_lin = (LinearLayout) findViewById(R.id.oldcall_lin);

		name = (EditText) findViewById(R.id.old_ed_name);//联系人名称
		phonenum = (EditText) findViewById(R.id.old_ed_phonenum);//电话号码

		oldcall_listview = (MyListView) findViewById(R.id.oldcall_listview);//数据列表



		next_btn.setOnClickListener(new OnClickListener() {//保存联系人

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				name1 = name.getText().toString();
				phonenum1 = phonenum.getText().toString();

				if(TextUtils.isEmpty(name1)){
					showToast("联系人不可为空");
				}else if(TextUtils.isEmpty(phonenum1)){
					showToast("手机号不可为空");
				}else if(phonenum1.length() != 11){
					showToast("手机号格式不正确");
				}else{
					isCheckMapadd.put("\"name\"","\""+name1+"\"");
					isCheckMapadd.put("\"num\"","\""+phonenum1+"\"");
					List list1 = new ArrayList();
					list1.add(isCheckMapadd);
					list1.addAll(list);
					String data =list1+"";
					provider.requestOldUpdatePhone(imei,data.replace(" ","").replace("=",":"),"sos","OldUpdatePhone");

					OldSosAdapter adapter = new OldSosAdapter(OldSettongSosActivity.this,list1);
					oldcall_listview.setAdapter(adapter);
				}
			}
		});

	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		if (actionName.equals("OldSos")) {

			if("{}".equals(object)){
				showToast("无数据 请添加数据");
				oldcall_lin.setVisibility(View.VISIBLE);

			}else{

				oldcall_lin.setVisibility(View.GONE);

				try {
					JSONObject jsonObject = new JSONObject(object+"");

					JSONArray jsonArray = jsonObject.getJSONArray("sos");
					for(int i = 0;i < jsonArray.length();i++){

						String jsonarray = jsonArray.getString(i);

						JSONObject jsonObject2 = new JSONObject(jsonarray);
						//						String no = jsonObject2.getString("no");
						String num = jsonObject2.getString("num");
						String name = jsonObject2.getString("name");
						isCheckMap = new HashMap<String, String>();
						//						isCheckMap.put("\"no\"","\""+no+"\"");
						isCheckMap.put("\"num\"","\""+num+"\"");
						isCheckMap.put("\"name\"","\""+name+"\"");
						if(num.equals("") && name.equals("")){

						}else{
							list.add(isCheckMap);
						}
					}

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				adapter = new OldSosAdapter(OldSettongSosActivity.this,list);
				oldcall_listview.setAdapter(adapter);

				adapter.setOnMyClickListener1(new OnMyClickListener1() {

					@Override
					public void onClick(int position, String name,
							String phone, String flag) {
						// TODO Auto-generated method stub

						if(TextUtils.isEmpty(name)){
							showToast("联系人不可为空");
						}else if(TextUtils.isEmpty(phone)){
							showToast("手机号不可为空");
						}else if(phone.length() != 11){
							showToast("手机号格式不正确");
						}else{

							isCheckMapadd.put("\"name\"","\""+name+"\"");
							isCheckMapadd.put("\"num\"","\""+phone+"\"");
							List list1 = new ArrayList();
							list1.addAll(list);
							list1.add(position+1,isCheckMapadd);
							String data =list1+"";
							provider.requestOldUpdatePhone(imei,data.replace(" ","").replace("=",":"),"sos","OldUpdatePhone");

							adapter = new OldSosAdapter(OldSettongSosActivity.this,list1);
							oldcall_listview.setAdapter(adapter);

						}
					}

				});
				adapter.setOnMyClickListener(new OnMyClickListener() {

					@Override
					public void onClick(int position, String flag) {
						// TODO Auto-generated method stub
						
						list.remove(position);
						
						adapter.notifyDataSetChanged();
						
						String data = list+"";
						
						provider.requestOldUpdatePhone(imei,data.replace(" ","").replace("=",":"),"sos","DelUpdatePhone");

					}
				});

			}


		}


		if(actionName.equals("OldUpdatePhone")){


			if("true".equals(object)){
				showToast("添加快捷拨号成功");
				name.setText("");
				phonenum.setText("");

			}

		}
		if(actionName.equals("DelUpdatePhone")){
			if("true".equals(object)){
				showToast("删除成功");
				name.setText("");
				phonenum.setText("");
			}

		}
	}


}
